package com.hbu.gateway.hbumallgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * @ProjectName: hbu-mall
 * @Package: com.hbu.gateway.hbumallgateway.config
 * @ClassName: HBUCorsConfiguration
 * @Author: zxh
 * @Description: 解决跨域问题
 * @Date: 2022/11/3 22:33
 * @Version: 1.0
 */
@Configuration
public class HbuCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许哪些头进行跨域
        corsConfiguration.addAllowedHeader("*");
        // 允许哪些请求方式进行跨域
        corsConfiguration.addAllowedMethod("*");
        // 允许哪些请求来源允许跨域
        corsConfiguration.addAllowedOriginPattern("*");
        // 允许携带cookie进行跨域
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
